package web;

import java.util.Date;
import java.util.List;

import tool.DateUtils;
import vo.Opinion;
import vo.OpinionType;
import vo.SentenceType;
import business.OpinionService;
import business.OpinionTypeService;
import business.PaperService;
import business.SentenceTypeService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class OpinionAction implements Preparable {
	private OpinionService service;
	private PaperService paperService;
	private OpinionTypeService opinionTypeService;

	private List<Opinion> opinions;
	private List<OpinionType> opinionTypes;

	// 添加sentence
	private int paperId;
	private String text;
	private int opinionTypeId;

	private String keyword;//检索条件

	// 分页的变量
	private int curPage;
	private int totalPage;
	private int pageSize = 10;
	private int totalCount;

	private String paperTitle;

	public String insertOpinion() {
		System.out.println("paperId:" + paperId);
		System.out.println("opinionTypeId:" + opinionTypeId);
		System.out.println("text:" + text);

		Opinion op = new Opinion();
		OpinionType opt = opinionTypeService.getById(opinionTypeId);
		op.setOpinionType(opt);
		op.setText(text);
		op.setPaper(paperService.getById(paperId));
		Date today = new Date();
		String createdAt = DateUtils.date2Str(today, "-");
		op.setCreatedAt(createdAt);
		service.insertOpinion(op);
		return "insertOpinion";
	}

	public String paperOpinion() {
		// load opinion类别
		opinionTypes = opinionTypeService.getAll();
		System.out.println("opinionTypes:" + opinionTypes.size());

		paperTitle = paperService.getById(paperId).getTitle();
		System.out.println("paper id:" + paperId);
		System.out.println("paper title:" + paperTitle);

		if (curPage == 0)
			curPage = 1;
		totalCount = (int) service.getTotalCountByPaperId(paperId);
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		this.opinions = service.getByPaperId((curPage - 1) * pageSize, pageSize, paperId);
		System.out.println("opinion count:" + this.opinions);

		return Action.SUCCESS;
	}
//	// 按照类型查找opinions
//	public String listOpinion() {
//		// load opinion类别
//		this.opinionTypes = opinionTypeService.getAll();
//
//		if (curPage == 0)
//			curPage = 1;
//		if (this.opinionTypeId == 0) {
//			totalCount = (int) service.getTotalCount();
//			if (totalCount % pageSize == 0)
//				totalPage = totalCount / pageSize;
//			else
//				totalPage = totalCount / pageSize + 1;
//			this.opinions = service.getAll((curPage - 1) * pageSize, pageSize);
//			System.out.println("opinion count:" + this.opinions.size());
//		} else {
//			totalCount = (int) service.getTotalCountByType(this.opinionTypeId);
//			if (totalCount % pageSize == 0)
//				totalPage = totalCount / pageSize;
//			else
//				totalPage = totalCount / pageSize + 1;
//			this.opinions = service.getByType((curPage - 1) * pageSize, pageSize, this.opinionTypeId);
//
//			System.out.println("opinion count:" + this.opinions.size());
//
//		}
//
//		return Action.SUCCESS;
//	}

	public String listOpinion() {
		// load句子类别
		System.out.println("sentence type,keyword:" + this.opinionTypes + "," + this.keyword);
		this.opinionTypes = opinionTypeService.getAll();
		if (curPage == 0)
			curPage = 1;

		// 没有关键字，没有类别
		if (keyword == null && opinionTypeId == 0) {
			totalCount = (int) service.getTotalCount();
			this.opinions = service.getAll((curPage - 1) * pageSize, pageSize);
		}
		// 有关键字，没有类别
		else if (keyword != null && opinionTypeId == 0) {
			totalCount = (int) service.getTotalCountByKeyword("%"+keyword+"%");
			this.opinions = service.getByKeyword((curPage - 1) * pageSize, pageSize, "%"+keyword+"%");
		}// 没哟关键字，有类别
		else if (keyword == null && opinionTypeId != 0) {
			totalCount = (int) service.getTotalCountByType(opinionTypeId);
			this.opinions = service.getByType((curPage - 1) * pageSize, pageSize, opinionTypeId);
		}// 有关键字和类别
		else if (keyword != null && opinionTypeId != 0) {
			totalCount = (int) service.getTotalCountByTypeKeyword(opinionTypeId, "%"+keyword+"%");
			this.opinions = service.getByTypeKeyword((curPage - 1) * pageSize, pageSize, opinionTypeId, "%"+keyword+"%");
		}

		// 计算总页数
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("sentence count:" + this.opinions.size());

		return Action.SUCCESS;
	}
	public String searchOpinion() {
		System.out.println("opinionTypeId:" + opinionTypeId);
		System.out.println("keyword:" + keyword);
		return "searchOpinion";
	}

	public void prepare() throws Exception {
		// if (id != null)
		// employee = service.find(id);
	}

	public void setOpinionService(OpinionService service) {
		this.service = service;
	}

	public int getPaperId() {
		return paperId;
	}

	public void setPaperId(int paperId) {
		this.paperId = paperId;
	}

	public void setService(OpinionService service) {
		this.service = service;
	}

	public void setPaperService(PaperService paperService) {
		this.paperService = paperService;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<OpinionType> getOpinionTypes() {
		return opinionTypes;
	}

	public void setOpinionTypes(List<OpinionType> opinionTypes) {
		this.opinionTypes = opinionTypes;
	}

	public int getOpinionTypeId() {
		return opinionTypeId;
	}

	public void setOpinionTypeId(int opinionTypeId) {
		this.opinionTypeId = opinionTypeId;
	}

	public void setOpinionTypeService(OpinionTypeService opinionTypeService) {
		this.opinionTypeService = opinionTypeService;
	}

	public String getPaperTitle() {
		return paperTitle;
	}

	public void setPaperTitle(String paperTitle) {
		this.paperTitle = paperTitle;
	}

	public List<Opinion> getOpinions() {
		return opinions;
	}

	public void setOpinions(List<Opinion> opinions) {
		this.opinions = opinions;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

}